package com.itboye.ihomebank.key;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.YongHuGuanLiAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.KeyOfLockBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.YongHuGuanLiBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 钥匙用户管理
 * 
 */
public class ActivityYongHuGuanLi extends BaseOtherActivity implements Observer {
	XListView yonghu_listview;

	ImageView key_title_finish;
	TextView key_title_title;
	RelativeLayout key_title_xuanze;
	TextView key_title_xuan;
	
	ImageView key_title_shezhi;//下拉的那个框

	PopupWindow window;

	YongHuGuanLiAdapter adapter;

	List<YongHuGuanLiBean> list;
	UserPresenter userPresenter;

	private String lockId;
	TextView nodata;
	private String uid;

	private List<KeyOfLockBean> beans;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.key_title_finish:
			finish();
			break;
//		case R.id.key_title_xuanze:// 切换
//			PuPoWindow(v);
//			break;

		default:
			break;
		}

	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_yonghu_guanli;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		key_title_title.setText("用户管理");
		key_title_xuan.setText("电子钥匙");
		key_title_shezhi.setVisibility(View.GONE);
		
		userPresenter = new UserPresenter(this);
		uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
		lockId = getIntent().getStringExtra("lockid");
		
		list = new ArrayList<YongHuGuanLiBean>();
		yonghu_listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ActivityYongHuGuanLi.this,
						ActivityKaiSuoJiLu.class);
				intent.putExtra("lockid", beans.get(position - 1).getLockId());
				intent.putExtra("touid", beans.get(position - 1).getUid());
				intent.putExtra("keyid", beans.get(position - 1).getKeyId());
				intent.putExtra("status", beans.get(position - 1).getStatus());
				startActivity(intent);
			}
		});

	}
	@Override
	protected void onResume() {
		super.onResume();
		userPresenter.keyOfLock(uid, lockId);
	}

	public void refresh(String keyId, int type) {
		Iterator<KeyOfLockBean> iterator = beans.iterator();
		while (iterator.hasNext()) {
			KeyOfLockBean keyOfLockBean = (KeyOfLockBean) iterator.next();
			if (keyOfLockBean.getKeyId().equals(keyId)) {
				if (type == 0) {
					keyOfLockBean.setKeyStatus("分享钥匙，等待下载");
					adapter.notifyDataSetChanged();
				} else {
					keyOfLockBean.setKeyStatus("已冻结");

				}
				break;
			}
		}
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	/**
	 * 切换框
	 * 
	 * @param v
	 */
	private void PuPoWindow(View v) {

		View view = LayoutInflater.from(this).inflate(
				R.layout.item_yonghu_qiehuan, null);
		TextView yopnghu_yaoshi = (TextView) view
				.findViewById(R.id.yopnghu_yaoshi);
		TextView yopnghu_pass = (TextView) view.findViewById(R.id.yopnghu_pass);

		yopnghu_yaoshi.setOnClickListener(this);
		yopnghu_pass.setOnClickListener(this);

		window = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		window.setOutsideTouchable(true);
		window.setBackgroundDrawable(new BitmapDrawable());
		window.showAsDropDown(v);
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == UserPresenter.keyOfLock_success) {
				beans = (List<KeyOfLockBean>) entity.getData();
				if (beans != null) {
					if (beans.size() > 0) {
						nodata.setVisibility(View.GONE);
						yonghu_listview.setVisibility(View.VISIBLE);
						adapter = new YongHuGuanLiAdapter(this, beans,
								R.layout.item_yonghu_guanli);
						yonghu_listview.setAdapter(adapter);
					} else {
						nodata.setVisibility(View.VISIBLE);
						yonghu_listview.setVisibility(View.GONE);
					}
				} else {
					nodata.setVisibility(View.VISIBLE);
					yonghu_listview.setVisibility(View.GONE);
				}
			} else if (entity.getEventType() == UserPresenter.keyOfLock_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}

}
